package com.itheima;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

public class ArrayListDemo {
    public static void main(String[] args) {
        //1.创建ArrayList对象
        ArrayList<String> list = new ArrayList<String>();

        //2.添加元素
        list.add("Hello");
        list.add(0, "World");
        //world hello

        //3.获取元素
        String s = list.get(1);
        System.out.println(s);

        //4.修改元素
        list.set(0, "Java");

        //5.删除元素
        list.remove(0);

        System.out.println(list);


        //遍历元素

        //1.普通for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        //2.增强for循环
        for (String obj : list) {
            System.out.println(obj);
        }

        //3.迭代器
        //获取集合的迭代器对象
        Iterator<String> iterator=list.iterator();
        //boolean hasNext = iterator.hasNext();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }

        //4.使用Lambda表达式
        //lambda表达式本质是一个函数：
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        list.forEach(name-> System.out.println(name));

        list.forEach(System.out::println);

    }
}
